// 加载Express模块
const express = require('express');

// 加载MySQL模块
const mysql = require('mysql');

// 加载bodyParser模块
const bodyParser = require('body-parser');

// 加载MD5模块
const md5 = require('md5');

// 创建MySQL连接池
const pool = mysql.createPool({
  host: '127.0.0.1',   //MySQL服务器地址
  port: 3306,          //MySQL服务器端口号
  user: 'root',        //数据库用户的用户名
  password: '',        //数据库用户密码
  database: 'nav',    //数据库名称
  connectionLimit: 20, //最大连接数
  charset: 'utf8'      //数据库服务器的编码方式
});

// 创建服务器对象
const server = express();

server.use(bodyParser.urlencoded({
  extended: false
}));


// 加载CORS模块
const cors = require('cors');
const { count } = require('console');

// 使用CORS中间件
server.use(cors({
  origin: ['http://localhost:8080', 'http://127.0.0.1:8080','http://localhost:8081','http://localhost:8082']
}));


//添加路由(接口)
//1.用户注册接口
//接口地址：http://127.0.0.1:3030/index/bar
//请求方式：get
server.get('/bar', (req, res) => {
	var obj = req.query
	pool.query('select * from indexs_Bar', [obj], (err, r) => {
		if (err) {
			throw err
		}
		console.log(r)
		res.send(r)

	})
})
//添加路由(接口)
//1.用户注册接口
//接口地址：http://127.0.0.1:3030/index/select
//请求方式：get
server.get('/select', (req, res) => {
	var obj = req.query
	pool.query('select * from index_Select', [obj], (err, r) => {
		if (err) {
			throw err
		}
		console.log(r)
		res.send(r)

	})
})
//添加路由(接口)
//1.用户注册接口
//接口地址：http://127.0.0.1:3030/index/program
//请求方式：get
server.get('/program', (req, res) => {
	var obj = req.query
	pool.query('select * from index_Program', [obj], (err, r) => {
		if (err) {
			throw err
		}
		console.log(r)
		res.send(r)

	})
})
//添加路由(接口)
//1.用户注册接口
//接口地址：http://127.0.0.1:3030/index/footer
//请求方式：get
server.get('/footer', (req, res) => {
	var obj = req.query
	pool.query('select * from index_Footer', [obj], (err, r) => {
		if (err) {
			throw err
		}
		console.log(r)
		res.send(r)

	})
})

//添加路由(接口)
//1.用户注册接口
//接口地址：http://127.0.0.1:3030/support/card
//请求方式：get
server.get('/card', (req, res) => {
  var obj = req.query
  pool.query('select * from support_Card', [obj], (err, r) => {
    if (err) {
      throw err
    }
    console.log(r)
    res.send(r)

  })
})
//添加路由(接口)
//1.用户注册接口
//接口地址：http://127.0.0.1:3030/support/footer
//请求方式：get
server.get('/footer', (req, res) => {
  var obj = req.query
  pool.query('select * from support_Footer', [obj], (err, r) => {
    if (err) {
      throw err
    }
    console.log(r)
    res.send(r)

  })
})




// -----------------------------------------------------------







// 获取全部数据的接口
server.get('/laptop', (req, res) => {

  // SQL查询
  let sql = "select * from laptop,laptop_pic where lid=pid";

  // 执行SQL查询
  pool.query(sql, (error, results) => {
    if (error) throw error;

    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results });
  });
});


// 查询所有商品图片表图片
server.get('/laptop_pic', (req, res) => {

  // SQL查询
  let sql = "select * from laptop_pic";

  // 执行SQL查询
  pool.query(sql, (error, results) => {
    if (error) throw error;

    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results });
  });
});


// 查询3080的电脑
server.get('/indexProduct1', (req, res) => {

  // SQL查询
  let sql = "select * from index_product where clid=3080";

  // 执行SQL查询
  pool.query(sql, (error, results) => {
    if (error) throw error;

    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results });
  });
});






// 查询3060的电脑
server.get('/indexProduct2', (req, res) => {

  // SQL查询
  let sql = "select * from index_product where clid=3060";

  // 执行SQL查询
  pool.query(sql, (error, results) => {
    if (error) throw error;

    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results });
  });
});



//用户注册接口
server.post('/register', (req, res) => {
  //console.log(md5('12345678'));
  // 获取用户名和密码信息
  let uname = req.body.uname;
  let upwd = req.body.upwd;
  let age = req.body.age
  console.log(upwd);
  console.log(uname);
  console.log(age);
  //以uname为条件进行查找操作，以保证用户名的唯一性
  let sql = 'select * from user where uname=?';
  pool.query(sql, [uname], (error, result) => {
    if (error) throw error;
    if (result.length == 0) {
      // 将用户的相关信息插入到数据表
      sql = 'INSERT user(uname,upwd,age) VALUES(?,?,?)';
      pool.query(sql, [uname, upwd, age], (error, result) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200 });
      })
    } else {
      res.send({ message: 'user exists', code: 201, result: result });
    }
  });
});

// 用户登录接口
server.post('/login', (req, res) => {
  //获取用户名和密码信息
  let uname = req.body.uname;
  let upwd = req.body.upwd;
  console.log(uname);
  console.log(upwd);
  // SQL语句
  //             查询用户信息,avatar是头像的意思
  // 'select * from user where uname=? and upwd=MD5(?)';;
  let sql = 'select * from user where uname=? and upwd=?';
  pool.query(sql, [uname, upwd], (error, result) => {
    if (error) throw error;
    console.log(upwd);
    
    if (result.length == 0) { //登录失败
      res.send({ message: '登录失败 login failed', code: 201 });console.log(result.length);
    } else {                 //登录成功
      res.send({ message: 'ok', code: 200, result: result[0] });console.log(result.length);
    }
  });

});


// 商品页面数据详情
server.get('/detail',(req, res)=>{
  let lid= req.query.lid*1
  console.log(lid)
  // let sql = 'select * from laptop_pic left outer join laptop on lid=laptop_id'
  // let sql = 'select * from laptop_pic,  laptop where lid='+lid
  let sql = 'select * from laptop where lid = '+lid
  // let sql  = 'select * from laptap'

  pool.query(sql,(error, results) => {
    if (error) throw error;
    console.log(results)
  
    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results });
  });
});


// 搜索功能, 
server.get('/search',(req, res)=>{
  let value = req.query.value
  console.log(value)
  // let sql = 'select * from laptop_pic left outer join laptop on lid=laptop_id'
  // let sql = 'select * from laptop_pic,  laptop where lid='+lid
  let sql = `select * from laptop where subtitle like '%${value}%'`
  pool.query(sql,(error, results) => {
    if (error) throw error;
    console.log(results)

    // 返回数据到客户端
    res.send({ message: 'ok', code: 200, result: results });
  });
});




//指定服务器对象监听的端口号
server.listen(3000, () => {
  console.log('server is running...');
});

